let express = require('express');
let router = express.Router();
var multer = require('../../components/multerUtil');
let filter = require('../../components/filter');

/**
 * 查询教材数据 by v 2018-06-29
 *
 * subjectId为空查询所有，否则查询指定subjectId下的教材数据
 */
router.get('/', filter.authorize, function (req, res, next) {
    let params = req.query;

    var limits = "";
    var sql = "";
    var datas = [];

    if (params.subjectId !== undefined && params.subjectId !== "") {
        sql = " where subjectId = ?";
        datas.push(params.subjectId);
    }

    req.app.db.query("select count(1) as totalCount from textbook " + sql, datas, function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results[0].totalCount === 0) {
            res.json({"success": true, "data": {}, "totalCount": 0});
            return;
        }

        let totalCount = results[0].totalCount;

        if (params.pageNo > 0 && params.pageSize > 0) {
            limits = " limit ?, ?";
            datas.push((params.pageNo - 1) * params.pageSize);
            datas.push(params.pageSize * 1);
        }

        req.app.db.query("select * from textbook " + sql + "order by id desc" + limits, datas, function (err, results, fields) {
            if (err) {
                res.json({"success": false, "message": global.databaseErr, "detail": err});
                return;
            }

            for (var i = 0; i < results.length; i++) {
                if (results[i].pdfFile !== "") {
                    results[i].pdfFile = global.contextPath + "/download/" + results[i].pdfFile;
                }
                results[i].uploadFile = {fileArray: []};
            }

            res.json({"success": true, "data": results, "totalCount": totalCount});
        });
    });
});

/**
 *
 * 按id查看指定的教材信息
 */
router.get('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query("select * from textbook where id = ?", [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results.length === 0) {
            res.json({"success": false, "message": global.noDataErr});
            return;
        }

        if (results[0].pdfFile !== "") {
            results[0].pdfFile = global.contextPath + "/download/" + esults[0].mediaFile;
        }

        res.json({"success": true, "data": results[0]});
    });
});

/**
 * 修改教材数据 by p 2018-06-29
 *
 *
 */
router.put('/:id', filter.authorize, multer.single("uploadFile"), function (req, res, next) {
    let params = req.body;
    var datas = [];

    var title = "";
    var pdfFile = "";

    if (params.id === undefined || params.id === '' || params.id <= 0) {
        res.json({"success": false, "message": "教材编码不正确！"});
        return;
    }

    if (params.subjectId === undefined || params.subjectId === '') {
        res.json({"success": false, "message": "所属章节编码不正确！"});
        return;
    }
    datas.push(params.subjectId);

    if (params.title !== undefined && params.title !== '') {
        title = params.title;
    }
    datas.push(title);

    if (req.file !== undefined) {
        pdfFile = req.file.filename;
        datas.push(pdfFile);
    }

    datas.push(params.id);

    req.app.db.query("update textbook set subjectId = ?, title = ?" + (pdfFile === "" ? "" : ", pdfFile = ?") + " where id = ?", datas, function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.putMsg});
    });
});

/**
 * 新增教材数据 by p 2018-06-11
 *
 *
 */
router.post('/', filter.authorize, multer.single("uploadFile"), function (req, res, next) {
    let params = req.body;

    var pdfFile = "";

    if (params.subjectId === undefined || params.subjectId === '') {
        res.json({"success": false, "message": "所属章节编码不正确！"});
        return;
    }

    if (req.file !== undefined) {
        pdfFile = req.file.filename;
    }

    req.app.db.query("insert into textbook(subjectId, title, pdfFile) values(?, ?, ?)", [params.subjectId, params.title, pdfFile], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.postMsg});
    });
});

/**
 * 删除教材信息 by v 2018-06-11
 *
 * 按id删除指定的教材信息
 */
router.delete('/:id', function (req, res, next) {
    let params = req.params;

    req.app.db.query('delete from textbook where id = ?', [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.deleteMsg});
    });
});

module.exports = router;